﻿using B.S.ProcessEgine.API.Applocations.Commands.FormDetail;
using B.S.ProcessEgine.API.Applocations.Commands.FormInfoCommand;
using B.S.ProcessEgine.API.Applocations.Commands.FormTypeInfo;
using B.S.ProcessEgine.API.Applocations.Commands.FormTypeInfoCommand;
using B.S.ProcessEgine.API.Applocations.Commands.GridProfessionalCommand;
using B.S.ProcessEgine.API.Dtos.RequestDto;
using B.S.ProcessEgine.API.Dtos.ResponseDto;
using B.S.ProcessEgine.Domain.Entities;
using B.S.ProcessEgine.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.ProcessEgine.API.Controllers
{
    /// <summary>
    /// 表单分类管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FormTypeInfoController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<FormTypeInfoController> logger;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public FormTypeInfoController(IMediator mediator, ILogger<FormTypeInfoController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 表单分类新增
        /// </summary>
        /// <param name="formTypeInfo">表单实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]


        public async Task<ErrorCode.ApiResult<int>> AddFormTypeInfo(FormTypeInfo formTypeInfo)
        {
            try
            {
                
                AddFormTypeInfoCommand cmd = new AddFormTypeInfoCommand()
                {
                    data = formTypeInfo
                };
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("表单添加出错" + ex.Message);
                throw;
            }
        }
        
        /// <summary>
        /// 表单分类列表 + 查询
        /// </summary>
        /// <param name="cmd">查询条件</param>
        /// <returns>返回符合条件的对象</returns>
        [HttpGet]
        public async Task<ErrorCode.ApiResult<ErrorCode.ApiPaging<PagingFormTypeInfoRespDto>>> PagingFormTypeInfo([FromQuery] PagingFormTypeInfoCommand cmd)
        {
            try
            {
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("表单分页列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        ///  表单分类删除
        /// </summary>
        /// <param name="cmd">表单id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ErrorCode.ApiResult> DelFormTypeInfo([FromQuery] DelFromTypeInfoCommand cmd)
        {
            try
            {
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("删除表单出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 表单分类修改
        /// </summary>
        /// <param name="formTypeInfo">表单对象</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ErrorCode.ApiResult> UpdateFormTypeInfo(FormTypeInfo formTypeInfo)
        {
            try
            {
                UpdateFormTypeInfoCommand cmd = new UpdateFormTypeInfoCommand()
                {
                    data = formTypeInfo
                };
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("表单修改出错" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 表单专业分类 下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ErrorCode.ApiResult<IList<GridProfessionalRespDto>>> FormTypeInfoSelect()
        {
            try
            {
                ProfessionalListCommand cmd = new ProfessionalListCommand();
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("表单专业类型列表出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 表单分类 下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetFormTypeReponseDto>>> GetFormTypeList()
        {
            try
            {
                GetFormTypeCommand cmd = new GetFormTypeCommand();
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("表单类型列表出错" + ex.Message);
                throw;
            }
        }










    }
}
